<?php
!function_exists('adminmsg') && exit('Forbidden');
include_once(M_P . 'config/field_config.php');
$secondHouseField = array('amenities','propertyright');
$housefieldsService = House::LoadService('HouseFieldsService');/* @var $housefieldsService PW_HouseFieldsService*/
if (empty($action)) {
	require_once PrintMode('fields');
} elseif ($action == 'field') {
	InitGP(array('id','page'));
	if (!$flag = $housefieldsService->checkIn($id)) adminmsg('undefined_action');
	$fieldtype = $housefieldsService->getFieldType($id, $flag);
	if (empty($_POST['step'])) {
		InitGP(array('page'), '', 2);
		$page < 1 && $page = 1;
		$perpage = 20;
		$count = $housefieldsService->getCountFieldList($id);
		$maxVieworder = $housefieldsService->getMaxVieworder($id);
		$numofpage = ceil($count / $perpage);
		$page > $numofpage && $page = $numofpage;
		$pages = numofpage($count, $page, $numofpage, "$basename&action=field&id=$id&");
		$fielddb = $housefieldsService->getFieldListByType($id,$page,$perpage);
		if($id == 'plate') $areaList = $housefieldsService->getAllAreaList();
		require_once PrintMode('fields');
	} else {
		$basename .= '&action=field&id=' . $id;
		if ($fieldtype == 'area') {
			define('AJAX', 1);
			InitGP(array('name','code'));
			InitGP(array('ifopen'), '', 2);
			//strlen($code) > 8 && adminmsg('区域代码长度不能超过8位');
			$data = array(
				'code'		=> mt_rand(100000,999999),
				'name'		=> trim($name),
				'ifopen'	=> $ifopen
			);
		}elseif($fieldtype == 'range'){
			InitGP(array('vstart', 'vend','fieldvalue'));
			checkRange($vstart,$vend);
			$data = array( $id,$vstart, $vend,$fieldvalue);
		} else {
			define('AJAX', 1);
			InitGP(array('fieldvalue','vieworder','ifopen','area'));
			$data = array(
				'fieldvalue'	=> trim($fieldvalue),
				'vieworder'		=> $vieworder,
				'ifopen'		=> $ifopen
			);
			if($id == 'plate') $data['parent'] = $area;
		}
		$return = $housefieldsService->addField($data,$id);
		if ( $return !== true )  adminmsg($return);
		$housefieldsService->updateFieldsCache();
		if(defined('AJAX')) Showmsg('ajaxma_success');
		else adminmsg('operate_success');
	}
} elseif ($action == 'edit') {
	InitGP(array('type','id'));
	define('AJAX', 1);
	$fields = $housefieldsService->getFieldsById($type, $id);
	empty($fields) && adminmsg('undefined_action');
	if (empty($_POST['step'])) {
		ifcheck($fields['ifopen'], 'ifopen');
		$posthash = EncodeUrl("$basename&action=edit", true);
		if($type == 'plate') $areaList = $housefieldsService->getAllAreaList();
		require_once PrintMode('ajax_fields');
		ajax_footer();
	} else {
		InitGP(array('fieldvalue','vieworder','ifopen','area'));
		$data = array(
			'fieldvalue'	=> trim($fieldvalue),
			'vieworder'		=> $vieworder,
			'ifopen'		=> $ifopen
		);
		if($type == 'plate') $data['parent'] = $area;
		if (($ret = $housefieldsService->editFieldById($type, $id, $data)) !== true) adminmsg($ret);
		$housefieldsService->updateFieldsCache();
		Showmsg('ajaxma_success');
	}
} elseif ($action == 'editarea') {
	InitGP(array('id'), '', 2);
	define('AJAX', 1);
	$fields = $housefieldsService->getAreaById($id);
	empty($fields) && adminmsg('undefined_action');
	if (empty($_POST['step'])) {
		ifcheck($fields['ifopen'], 'ifopen');
		$posthash = EncodeUrl("$basename&action=editarea", true);
		require_once PrintMode('ajax_fields');
		ajax_footer();
	} else {
		InitGP(array('code','name','ifopen'));
		strlen($code) > 8 && adminmsg('区域代码长度不能超过8位');
		$data = array(
			'code'		=> $code,
			'name'		=> trim($name),
			'ifopen'	=> $ifopen
		);
		if (!$ifopen && $housefieldsService->getPlateByAreaId($id)) adminmsg('选项已被使用，不能停用!');
		if (($ret = $housefieldsService->editAreaById($id, $data)) !== true) adminmsg($ret);
		$housefieldsService->updateFieldsCache();
		Showmsg('ajaxma_success');
	}
} elseif ($action == 'delfield') {
	define('AJAX', 1);
	InitGP(array('id','type'));
	$posthash = EncodeUrl("$basename&action=operate", true);
	require_once PrintMode('ajax_fields');ajax_footer();
} elseif ($action == 'operate') {
	InitGP(array('id'));
	if (!$flag = $housefieldsService->checkIn($id)) adminmsg('undefined_action');
	$fieldtype = $housefieldsService->getFieldType($id, $flag);
	$basename .= '&action=field&id=' . $id;
	if ($fieldtype == 'area') {
		InitGP(array('selid','vieworder'), 'P', 2);
		if(empty($selid)) adminmsg('请选择要操作的区域',"$basename");
		if ($selid) {
			if ($_POST['job'] == 'del') {
				if ($housefieldsService->getPlateByAreaId($selid[0])  && !in_array($id,$secondHouseField)) adminmsg('选项已被使用，不能删除!');
				$housefieldsService->deleteField($selid,$id);
			} else {
				if ($_POST['job'] == 'close' && !in_array($id,$secondHouseField)) {
					if ($housefieldsService->getPlateByAreaId($selid[0])) adminmsg('选项已被使用，不能停用!');
				}
				$data = array('ifopen' => $_POST['job'] == 'open' ? 1 : 0);
				$housefieldsService->editField($data,$selid,$id);
			}
		}
	} else {
		InitGP(array('selid','vieworder'), 'P', 2);
		if(empty($selid)) adminmsg('请选择要操作的的字段',"$basename");
		if ($selid) {
			if ($_POST['job'] == 'del') {
				if ($flag == 'building' && !in_array($id,$secondHouseField)) {
					/*$houseinfo = House::LoadClass('HouseInfo');
					if ($houseinfo->checkFieldData($id, $selid, $fieldtype)) {
						adminmsg('选项已被使用，不能删除!');
					}*/
				}
				$housefieldsService->deleteField($selid,$id);
			} else {
				if (!in_array($id,$secondHouseField) && $_POST['job'] == 'close' && $flag == 'building') {
					/*$houseinfo = House::LoadClass('HouseInfo');
					if ($houseinfo->checkFieldData($id, $selid, $fieldtype)) {
						adminmsg('选项已被使用，不能停用!');
					}*/
				}
				$data = array('ifopen' => $_POST['job'] == 'open' ? 1 : 0);
				$housefieldsService->editField($data, $selid, $id);
			}
		}
		if ($vieworder && $vids = array_keys($vieworder)) {
			$tmp_arr = $housefieldsService->getFieldsByIds($id, $vids);
			foreach ($tmp_arr as $key => $value) {
				if ($value['vieworder'] != $vieworder[$value['id']]) {
					$housefieldsService->editField(array('vieworder' => $vieworder[$value['id']]), $value['id'], $id);
				}
			}
		}
	}
	$housefieldsService->updateFieldsCache();
	adminmsg('operate_success');
} elseif ($action == 'area') {
	if (empty($_POST['step'])) {
		InitGP(array('province', 'city','page'));
		$urladd .= $province ? $province : '';
		$urladd .= $province && $city ? $city : '';
		$perpage = 30;
		list($provincedb,$citydb,$areadb,$list,$ids,$j) = $housefieldsService->getAreaOfChina($province,$city,$page,$perpage);
		$pages = numofpage($j, $page, ceil($j / $perpage), "$basename&action=area{$urladd}&");
		$jscity = pwJsonEncode($citydb);
		require_once PrintMode('fields');

	} else {
		InitGP(array('province', 'city','selid', 'page'));
		if(empty($selid)) adminmsg('请选择要导入的区域',"$basename&action=area");
		$basename .= "&action=area&province=$province&city=$city&page=$page";
		$housefieldsService->importAreaList($selid);
		$housefieldsService->updateFieldsCache();
		adminmsg('operate_success');
	}
}

function checkRange($vstart,$vend){
	foreach ($vstart as $key => $value) {
		if ($value && $vend[$key] && $value > $vend[$key]) {
			adminmsg("区间起始值($value)不能大于结束值({$vend[$key]})!");
		}
		if($value && $vend[$key]){
			foreach($vstart as $subkey=>$subvalue){
				$where_one = ($value >= $subvalue && $vend[$key] <= $vend[$subkey]);
				$where_two = ($vend[$key] > $subvalue && $vend[$key] <= $vend[$subkey]);
				if($subkey != $key && ($where_one || $where_two) && $subvalue && $vend[$subkey]){
					$msg = "区间{$value}~{$vend[$key]}与区间{$subvalue}~{$vend[$subkey]}产生交集";
					adminmsg($msg);
				}
			}
		}
	}
}

if (!function_exists('str_split')) {
	function str_split($string, $split_length = 1) {
		$array = array();
		$strlen = strlen($string);
		if ( $split_length < 1 ) return false;
		if ( $split_length >= $strlen ) return array($string);
		for ( $i = 0, $key = 0, $count = 0; $i < $strlen; $i++, $count++ ) {
			if ( $count >= $split_length ) {
				$count = 0;
				$key++;
			}
			if ( isset($array[$key]) ) {
				$array[$key] .= $string{$i};
			} else {
				$array[$key] = $string{$i};
			}
		}
		return $array;
	}
}

?>